<?php 
/**
 *  
 */
date_default_timezone_set("Asia/Shanghai");

set_time_limit(0);

if (isset ( $_SERVER ['REQUEST_URI'] )) {
    die ( 'error' );
}
if (! defined ( 'API_ROOT_PATH' )) {
    define ( 'API_ROOT_PATH', dirname ( __FILE__ ) );
}

require_once (API_ROOT_PATH . '/../inc/Config.php');
require_once (API_ROOT_PATH . '/../inc/dblink.php');
require_once (API_ROOT_PATH . '/../inc/Mysql.php');

$db = new Mysql ( MYSQLDB, MYSQLDB_USER, MYSQLDB_PWD, MYSQLDB_DATABASE );

$start_num = $argv[1];

$end_num = $argv[2];

for ($i = 0; $i <= ($end_num - $start_num); $i++) { 

	$order_id = $i + $start_num;

	paoshuju($order_id);
}


function paoshuju($order_id){

	global $db;

	$order_sql = "select * from au_order where order_id = '".$order_id."'";

	$order_info = $db ->fetchOne($order_sql);

	if ($order_info['status'] < 6 || ($order_info['status'] == 12 && in_array($order_info['fail_type'], [1,2,3]))) {

		return 0;
	}
	else
	{
		$success_price = 0;

		$transfer_fee = 0;

		$lottery = 0;

		$coupon = 0;

		$commision = 0;

		$final_price = 0;

		$order_id = $order_info['order_id'];

		//拍卖最高价
		$success_price = $order_info['bidding_best_price'] > $order_info['bid_best_price'] ? $order_info['bidding_best_price'] : $order_info['bid_best_price'];

		//过户费
		if ($order_info['transfer_type'] == 0) {

			$transfer_fee = $order_info['transfer_fee'];
		}

		//抽奖立减
		$lottery_sql = "select c.order_id,c.state,c.price from au_scratch_card as c ,au_activity as a where c.a_id = a.aid and c.order_id = '".$order_id."' and a.type = 1";

		$lottery_info = $db ->fetchOne($lottery_sql);

		if (!empty($lottery_info)){

			if ($lottery_info['state'] > 0) {

				$lottery = $lottery_info['price'];

			}
		}

		//代金券
		$coupon_sql = "select sum(occur_money) as coupon_money from au_coupon_log  where dealer_id = '".$order_info['success_dealer_id']."' and order_id = '".$order_id."' and occur_type = 1 ";

		$coupon_info = $db ->fetchOne($coupon_sql);

		if (!empty($coupon_info)){

			if ($coupon_info['coupon_money'] > 0) {

				$coupon = $coupon_info['coupon_money'] < $order_info['coupon'] ? $coupon_info['coupon_money'] : $order_info['coupon'];
			}
		} 

		//交易服务费
		$commision = $order_info['commision'];

		//最终价格
		$final_price = $success_price + $commision + $transfer_fee -$lottery -$coupon;

		$insert_sql = "insert into au_order_fee (`order_id`,`success_price`,`commision`,`coupon`,`lottery`,`transfer_fee`,`final_price`,`createtime`) values ('".$order_id."','".$success_price."','".$commision."','".$coupon."','".$lottery."','".$transfer_fee."','".$final_price."',now())";

		$ok = $db ->query($insert_sql);
	}

}
$db->close();

unset($db);  

$db = null;